fileUrl <- "https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD"
# no se puede usar el argumento method="curl" en windows, solo en mac
#download.file(fileUrl,destfile="./data/cameras.csv")
list.files("./data")
# para luego poder guardar la variable y saber cuando se descargo la data
dateDownloaded <- date()
cameraData <- read.table("./data/cameras.csv",sep=",",header=TRUE)
head(cameraData)

# ahora en formato xlsx
library(xlsx)
#fileUrl <- "https://data.baltimorecity.gov/api/views/dz54-2aru/rows.xlsx?accessType=DOWNLOAD"
#download.file(fileUrl,destfile="./data/camera.xlsx")
#cameraData <- read.xlsx2("./data/camera.xlsx",sheetIndex=1)
#head(cameraData)
con <- file("./data/cameras.csv","r")
cameraData <- read.csv(con)
close(con)
head(cameraData)

#
con <- url("http://simplystatistics.org","r")
simplyStats <- readLines(con)
close(con)
head(simplyStats)

# leemos JSON
library(RJSONIO)
fileUrl <- "https://data.baltimorecity.gov/api/views/dz54-2aru/rows.json?accessType=DOWNLOAD"
#download.file(fileUrl,destfile="./data/camera.json")
con = file("./data/camera.json")
jsonCamera = fromJSON(con)
close(con)
head(jsonCamera)

#write data
cameraData <- read.csv("./data/cameras.csv")
#quitamos la primera columna
tmpData <- cameraData[,-1]
write.table(tmpData,file="./data/camerasModified.csv",sep=",")
cameraData2 <- read.csv("./data/camerasModified.csv")
head(cameraData2)

# grabamos el objeto R binario
cameraData <- read.csv("./data/cameras.csv")
tmpData <- cameraData[,-1]
save(tmpData,cameraData,file="./data/cameras.rda")
#leemos el objeto binario
# Remove everything from the workspace
rm(list=ls())
ls()
# Load data
load("./data/cameras.rda")
ls()

#parseamos html
library(XML)
html3 <- htmlTreeParse("http://scholar.google.com/citations?user=HI-I6C0AAAAJ&hl=en", useInternalNodes=T)
xpathSApply(html3, "//title", xmlValue)
xpathSApply(html3, "//td[@id='col-citedby']", xmlValue)
                  